<%@include file="_header.jspf" %>
<%@page import="com.spartancoder.accommodation.models.University" %>
<%@page import="com.spartancoder.accommodation.beans.UniversityBean" %>
<%
String error = null;

String name = request.getParameter("name");
String address = request.getParameter("address");

if( name != null && address != null ) {
    if( name.trim().isEmpty() ) {
        error = "Trebuie sa introduceti un nume";
    } else {
        if( University.exists(name) ) {
            error = "Exista deja o universitate cu acest nume";
        } else {
            UniversityBean ub = new UniversityBean();
            ub.setName(name);
            ub.setAddress(address);
            ub.setAvailable(true);
            University.create(ub);
            session.setAttribute("flash", "Universitatea a fost adaugata");
            response.sendRedirect("list-university.jsp");
        }
    }
} else {
    name = "";
    address = "";
}

%>

<h2>Adaugare universitate</h2>

<form action="add-university.jsp" method="post" class="span-12">
    <fieldset>
        <% if(error !=null) { %>
        <br /><div class="clear"><%=error%></div>
        <% } %>

        <label class="text large">Nume:</label><br />
        <input type="text" name="name" value="<%=name%>" class="text large" /><br />

        <label class="text large">Adresa</label><br />
        <textarea name="address" class="text large" style="height: 50px;"><%=address%></textarea>

        <button type="submit" class="button positive">
            Adauga universitatea
        </button>

    </fieldset>
</form>

<%@include file="_footer.jspf" %>